package pl.pelcra.cesar.spider.webparser.sites;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import pl.pelcra.cesar.spider.webcrawler.CSWebURL;
import pl.pelcra.cesar.spider.webparser.interfaces.BasePageParser;
import edu.uci.ics.crawler4j.url.UrlResolver;

public class OswPageParser extends BasePageParser
{

  public OswPageParser()
  {

  }

  public OswPageParser(String parameters, int politenessDelay)
  {
    super(parameters, politenessDelay);
  }

  @Override
  public void init(CSWebURL webURL, String language, String doc_id)
  {
    super.init(webURL, "body");

    if (super.isGetPageSuccess())
    {
      // super.setPageParserHelper(new IPageParserHelper() {
      //
      // @Override
      // public boolean filterContent(Element element) {
      // Elements el = element.select("a[target=_blank]");
      // if (el.size() == 0) {
      // el = element.select("a[target=_self]");
      //
      // if (el.size() == 0) {
      // return true;
      // }
      // }
      // return false;
      // }
      // });

      super.setContentTag("div[class=print-content]");
      this.setDoc_id(doc_id);
      this.setCategory("");
      super.setLanguage(language);
      super.setSource_id("OSW");
      super.setPage_dateTag("span[class=timestamp]");
      super.setParsed_pagedateFormat("yyyy-MM-dd");
      super.setTitleTag("h1[class=print-title]");
      super.setAuthorTag("span[class=authors]");
      super.setNotes("");
      this.setUrlAttachment("");
    }
  }

  @Override
  public void setCategory(String tagValue)
  {
    // http://www.osw.waw.pl/pl/publikacje/tydzien-na-wschodzie/2012-03-21/nowy-etap-w-relacjach-nato/usa-z-azja-centralna
    String url = getPage_url();
    String[] tabUrl = url.split("/");
    super.setCategory(tabUrl[6]);
  }

  @Override
  public void setDoc_id(String doc_id)
  {

    String separator = "/";
    String[] tabString = doc_id.split(separator);
    String regex = new String("\\d{4,4}-\\d{2,2}-\\d{2,2}");
    int startPos = 0;
    for (int i = 0; i < tabString.length; i++)
    {
      if (tabString[i].matches(regex))
      {
        startPos = i;
        break;
      }
    }

    String returnValue = new String();
    if (startPos == tabString.length - 1)
    {
      returnValue = tabString[startPos];
    } else
    {
      for (int i = startPos; i < tabString.length; i++)
      {
        returnValue += separator + tabString[i];
      }
    }

    super.setDoc_id(returnValue);
  }

  @Override
  public void setUrlAttachment(String tagValue)
  {
    Elements elements = super.getContentElements();
    boolean isAttachment = false;
    for (Element element : elements)
    {
      Element attachmentElement = this.getElement(element, tagValue);
      if (attachmentElement != null)
      {
        super.setUrlAttachment(UrlResolver.resolveUrl(attachmentElement.baseUri(),
            attachmentElement.attr("href")));
        this.addNotes("Full text in pdf.");
        isAttachment = true;
      }
    }
    if (!isAttachment)
    {
      Element mainElement = super.getMainElement();

      Element attachmentElement = this.getElement(mainElement, tagValue);
      if (attachmentElement != null)
      {
        super.setUrlAttachment(UrlResolver.resolveUrl(attachmentElement.baseUri(),
            attachmentElement.attr("href")));
        this.addNotes("Full text in pdf.");
        isAttachment = true;
      }
    }
  }
}
